﻿#region using statements

using System;
using System.IO;
using Microsoft.Xna.Framework;

#endregion

namespace XNAGameEngine.Logs
{
    public static class ManagerLogs
    {
        #region constructors

        /// <summary>
        /// Register the game engine
        /// If a game log backup file exists, it is deleted
        /// If a game log file exists, it is backed up and deleted
        /// </summary>
        /// <param name="game">the game engine</param>
        static ManagerLogs()
        {
            if (File.Exists("XNAEngineLog.txt"))
                File.Delete("XNAEngineLog.txt");
        }

        #endregion

        #region IManagerLogs Members

        /// <summary>
        /// Initialize a file to ensure that it will be overwritten
        /// </summary>
        /// <param name="fileName">file name to initialize</param>
        public static void InitializeFile(string fileName)
        {
            if (File.Exists(fileName))
                File.Delete(fileName);
        }

        /// <summary>
        /// Writes a string to the default log file, XNAGameEngineLog.txt
        /// </summary>
        /// <param name="input">string to write</param>
        public static void Write(string input)
        {
            //If the file exists, the StreamWriter does not need to be recreated
            if (File.Exists("XNAEngineLog.txt"))
            {
                File.AppendAllText(
                    "XNAEngineLog.txt",
                    "[" + DateTime.Now.TimeOfDay + "]:" + input + "\r\n");
                return;
            }

            //Get a StreamWriter and write string data
            //Creates the log file
            using (StreamWriter writer = File.CreateText("XNAEngineLog.txt"))
            {
                //appends the string argument to the log file
                //Creates the log file if it doesn't exist
                writer.Write("[" + DateTime.Now.TimeOfDay + "]:" + input + "\r\n");
            }
        }

        /// <summary>
        /// Write to a specific file
        /// </summary>
        /// <param name="fileName">file to write to</param>
        /// <param name="input">text to write</param>
        public static void Write(string fileName, string input)
        {
            //if the file exists, the StreamWriter does not need to be recreated
            if (File.Exists(fileName))
                File.AppendAllText(
                    fileName,
                    "[" + DateTime.Now.TimeOfDay + "]:" + input + "\r\n");

            //Get a StreamWriter and write string data
            //Creates the log file
            using (StreamWriter writer = File.CreateText(fileName))
            {
                //appends the string argument to the log file
                //Creates the log file if it doesn't exist
                writer.Write("[" + DateTime.Now.TimeOfDay + "]:" + " file initialized\r\n");
            }
        }

        #endregion
    }
}